﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NHibernate;

namespace ThorMVR.Tela_de_Pesquisa
{
    public partial class fPesquisa : Form
    {
        public String value;
        
        public fPesquisa()
        {
            InitializeComponent();
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.StartPosition = FormStartPosition.CenterScreen;
        }


        private void fPesquisa_Load(object sender, EventArgs e)
        {
          
            
        }

        public void pesquisaCnae()
        {
            IList<ClassModels.GeralCnae> cnae = new Pesquisa.PesquisasCadastro().pesquisaCnae();
            
            dg_Pesquisa.Columns.Add("cCNAE", "CNAE");
            dg_Pesquisa.Columns.Add("cDescricao", "Descrição");
            dg_Pesquisa.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
            dg_Pesquisa.Columns[1].Width = 410;


            if (cnae.Count != 0)
            {
                for (int i = 0; i <= (cnae.Count - 1); i++)
                {
                    dg_Pesquisa.Rows.Add(cnae[i]._codigo, cnae[i]._descricao);
                }

            }
            

        }



        public void pesquisaEmpresa()
        {
            ISession session = ClassModels.NHibernateHelper.GetCurrentSession();

            IQuery query = session.CreateQuery("FROM GeralEmpresas");

            IList<ClassModels.GeralEmpresas> empresa = query.List<ClassModels.GeralEmpresas>();

            dg_Pesquisa.Columns.Add("cNumeroEmpresa", "Codigo da Empresa");
            dg_Pesquisa.Columns.Add("cRazaoSocial", "Razão Social");
            dg_Pesquisa.Columns.Add("cCpfCnpj", "CPF/CNPJ");
            dg_Pesquisa.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
            dg_Pesquisa.Columns[1].Width = 410;
            dg_Pesquisa.Columns[2].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;

            if (empresa.Count != 0)
            {
                for (int i = 0; i <= (empresa.Count - 1); i++)
                {
                    dg_Pesquisa.Rows.Add(empresa[i]._id,empresa[i]._razaosocial,empresa[i]._cpf_cnpj);
                }

            }
            session.Close();
        }

        public void pesquisaNaturezaJuridica()
        {
            ISession session = ClassModels.NHibernateHelper.GetCurrentSession();

            IQuery query = session.CreateQuery("FROM GeralNaturezaJuridica");

            IList<ClassModels.GeralNaturezaJuridica> naturezaJuridica = query.List<ClassModels.GeralNaturezaJuridica>();

            dg_Pesquisa.Columns.Add("cNaturezaJuridica", "Codigo");
            dg_Pesquisa.Columns.Add("cDescricao", "Descrição");
            dg_Pesquisa.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
            dg_Pesquisa.Columns[1].Width = 410;


            if (naturezaJuridica.Count != 0)
            {
                for (int i = 0; i <= (naturezaJuridica.Count - 1); i++)
                {
                    dg_Pesquisa.Rows.Add(naturezaJuridica[i]._codigo, naturezaJuridica[i]._descricao);
                }

            }
            session.Close();
        }

        private void dg_Pesquisa_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            value = dg_Pesquisa.SelectedCells[0].OwningRow.Cells[0].Value.ToString();
            this.Hide();
        }

    }
}
